package com.imooc.bilibili.video.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bilibili.constant.common.RedisConstant;
import com.imooc.bilibili.domain.video.Danmu;
import com.imooc.bilibili.mapper.video.DanmuMapper;
import com.imooc.bilibili.util.StringUtils;
import com.imooc.bilibili.util.json.JsonUtils;
import com.imooc.bilibili.util.redis.RedisUtils;
import com.imooc.bilibili.video.IDanmuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 弹幕记录表 服务实现类
 * </p>
 *
 * @author gcq
 * @since 2022-06-04
 */
@Service
public class DanmuServiceImpl extends ServiceImpl<DanmuMapper, Danmu> implements IDanmuService {


    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private DanmuMapper danmuMapper;

    @Override
    public void addDanmusToRedis(Danmu danmu) {
        String key = "danmu-video:" + danmu.getVideoId();
        String value = redisUtils.get(key);
        List<Danmu> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(value)) {
            list = JsonUtils.jsonToList(value, Danmu.class);
        }
        list.add(danmu);
        redisUtils.set(key, JsonUtils.objectToJson(list));
    }

    @Override
    @Async
    public void addDanmus(Danmu danmu) {
        danmuMapper.insert(danmu);
    }

    /**
     * 查询策略优先查询redis中的弹幕数据
     * 如果没有的话查询数据库，然后把查询数据写入redis中
     *
     * @param videoId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Danmu> getDanMus(Long videoId, String startTime, String endTime) throws ParseException {
        String key = RedisConstant.DANMU_KEY + videoId;
        String value = redisUtils.get(key);
        List<Danmu> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(value)) {
            list = JsonUtils.jsonToList(value, Danmu.class);
            if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
                SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startDate = sft.parse(startTime);
                Date endDate = sft.parse(endTime);
                List<Danmu> childList = new ArrayList<>();
                for (Danmu danmu : list) {
                    Date createTime = danmu.getCreateTime();
                    // 是否在指定日期之后
                    // 之前日期之前
                    // 每天弹幕都是不一样的，需要查询最新的弹幕
                    if(createTime.after(startDate) && createTime.before(endDate)) {
                        childList.add(danmu);
                    }
                }
                list = childList;
            }
        } else {
            // redis中没有 则查询数据库
            list = danmuMapper.selectList(new QueryWrapper<Danmu>().
                    eq("videoId", videoId)
                    .ge("createTime", startTime)
                    .le("createTime", endTime));
            // 保存至redis中
            redisUtils.set(key, JsonUtils.objectToJson(list));
        }
        return list;
    }
}
